
#include "fsitemdelegate.h"
#include "multistageheader.h"

MultistageHeader::MultistageHeader(QWidget *parent) : QWidget(parent)
{
    ui.setupUi(this);

    m_pView = new QTableView(this);
    m_pView->setContextMenuPolicy(Qt::CustomContextMenu);
    // connect(m_pView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(tableContextMenuRequested(QPoint)));
    m_pHeader = new RbTableHeaderView(Qt::Horizontal, 3, 8, m_pView);
    QAbstractItemModel* pHeaderModel = m_pHeader->model();

    m_pHeader->setSpan(0, 0, 1, 4);
    m_pHeader->setSpan(0, 4, 1, 4);
    m_pHeader->setSpan(1, 0, 1, 2);
    m_pHeader->setSpan(1, 2, 1, 2);
    m_pHeader->setSpan(1, 4, 1, 2);
    m_pHeader->setSpan(1, 6, 1, 2);
    for (int i = 0; i < 8; i++) {
        m_pHeader->setSpan(2, i, 1, 1);
    }

    // 一级
    pHeaderModel->setData(pHeaderModel->index(0, 0), QString(u8"横向尺寸"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(0, 4), QString(u8"纵向尺寸"), Qt::DisplayRole);
    // 二级
    pHeaderModel->setData(pHeaderModel->index(1, 0), QStringLiteral("极耳宽度"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(1, 2), QStringLiteral("极耳高度"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(1, 4), QStringLiteral("极片宽度"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(1, 6), QStringLiteral("极耳间距"), Qt::DisplayRole);
    // 三级
    pHeaderModel->setData(pHeaderModel->index(2, 0), QStringLiteral("CCD测量值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 1), QStringLiteral("真值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 2), QStringLiteral("CCD测量值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 3), QStringLiteral("真值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 4), QStringLiteral("CCD测量值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 5), QStringLiteral("真值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 6), QStringLiteral("CCD测量值"), Qt::DisplayRole);
    pHeaderModel->setData(pHeaderModel->index(2, 7), QStringLiteral("真值"), Qt::DisplayRole);

    m_pHeader->setMinimumHeight(90);
    m_pHeader->setRowHeight(0, 30);
    m_pHeader->setRowHeight(1, 30);
    m_pHeader->setRowHeight(2, 30);
    // m_pHeader->setMinimumHeight(60);
    // int a = m_pHeader->height();
    m_pHeader->setSectionsClickable(false);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(0, 0), 0xcfcfcf);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(0, 4), 0xcfcfcf);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(1, 0), 0xcfcfcf);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(1, 2), 0xcfcfcf);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(1, 4), 0xcfcfcf);
    m_pHeader->setCellBackgroundColor(pHeaderModel->index(1, 6), 0xcfcfcf);
    for (int i = 0; i < 8; i++) {
        m_pHeader->setCellBackgroundColor(pHeaderModel->index(2, i), 0xcfcfcf);
    }

    int rowCount = 10;
    m_pDataModel = new QStandardItemModel;
    for (int i = 0; i < rowCount; i++) {
        QList<QStandardItem*> items;
        for (int j = 0; j < 8; j++) {
            items.append(new QStandardItem);
            m_pVecItems.append(items[j]);
        }

        m_pDataModel->appendRow(items);
    }

    m_pView->setModel(m_pDataModel);
    m_pView->setHorizontalHeader(m_pHeader);

    ReadOnlyDelegate *readonlydelegate = new ReadOnlyDelegate();
    m_pView->setItemDelegateForColumn(0, readonlydelegate);
    m_pView->setItemDelegateForColumn(2, readonlydelegate);
    m_pView->setItemDelegateForColumn(4, readonlydelegate);
    m_pView->setItemDelegateForColumn(6, readonlydelegate);

    if (!m_pLayout) {
        m_pLayout = new QHBoxLayout(this);
        this->setLayout(m_pLayout);
    }

    m_pLayout->addWidget(m_pView);
}

